package org.adorsys.aderp.jpa.impl;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.repository.core.RepositoryMetadata;

public class AderpRepositoryFactory extends JpaRepositoryFactory {

	private BeanFactory beanFactory;

	public AderpRepositoryFactory(EntityManager entityManager, BeanFactory beanFactory) {
		super(entityManager);
		this.beanFactory = beanFactory;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository(
			RepositoryMetadata metadata, EntityManager entityManager) {

		JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainClass());


		return new AderpJPARepository(entityInformation, entityManager, beanFactory);
	}

	@Override
	protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
		return AderpJPARepository.class;
	}

	
}
